package leetcode_day._2021._202106._0110;

import general_class.ListNode;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/6/5 20:01
 * 移除链表元素
 * 算法：链表
 * 链表题通常可以用递归进行求解
 */
public class _05_203 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(6);
        head.next.next.next = new ListNode(3);
        head.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next = new ListNode(5);
        head.next.next.next.next.next.next = new ListNode(6);
        System.out.println(new _05_203().removeElements(head, 6));
    }

    public ListNode removeElements(ListNode head, int val) {
        if (null == head) return head;
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;
    }

    public ListNode removeElements_two(ListNode head, int val) {
        if (null == head) return head;
        ListNode res = new ListNode();
        res.next = head;
        ListNode tmp = res;
        while (tmp.next != null) {
            if (tmp.next.val == val) tmp.next = tmp.next.next;
            else tmp = tmp.next;
        }
        return res.next;
    }

}
